package com.frame.library;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.tom.baselibrary.navigationbar.AbsNavigationBar;

/**
 * Builder设计模式，打造NavigationBar头部。一行实现头部。
 * 知识点：泛型 +Builder设计模式
 * 后续扩展：
 *      1.继承自AbsNavigationBar来实现新的类；
 *      2.继承自DefaultNavigationBar，来实现功能。
 * public public class DefaultNavigationBar<D extends  DefaultNavigationBar.Builder.DefaultNavigationParams> ...
 */
public class DefaultNavigationBar<D extends DefaultNavigationBar.Builder.DefaultNavigationParams> extends
        AbsNavigationBar<DefaultNavigationBar.Builder.DefaultNavigationParams> {

    public DefaultNavigationBar(Builder.DefaultNavigationParams params) {
        // 接受参数P，并传给父类中，然后创建+绑定。
        super(params);
    }

    @Override
    public void applyView() {
        // 3.绑定UI效果。设置点击事件。
        setImageResource(R.id.iv_left, getParams().mLeftResId);
        setImageResource(R.id.iv_right, getParams().mRightResId);
        setText(R.id.title, getParams().mTitle);
        setText(R.id.left_title, getParams().mLeftText);
        setText(R.id.right_title, getParams().mRightText);
        setBackgroundColor(R.id.title_bar, getParams().bgColor);
        setOnClickListener(R.id.left_title, getParams().mLeftClickListener);
        setOnClickListener(R.id.right_title, getParams().mRightClickListener);

        setVisibility(R.id.iv_left, getParams().leftIconVisible);
    }

    @Override
    public int bindLayoutId() {
        return R.layout.title_bar; // 返回写好的布局
    }

    public static class Builder extends AbsNavigationBar.Builder {
        DefaultNavigationParams P;

        public Builder(Context context) {
            super(context, null); // parent是空
            P = new DefaultNavigationParams(context, null);
        }
        public Builder(Context context, ViewGroup parent) {
            super(context, parent);
            P = new DefaultNavigationParams(context, parent);
        }


        // 1.设置所有效果，链式调用
        public Builder setTitle(String title) {
            P.mTitle = title;
            return this;
        }

        public Builder setLeftText(String leftText) {
            P.mLeftText = leftText;
            return this;
        }

        public Builder setRightText(String text) {
            P.mRightText = text;
            return this;
        }

        public Builder setRightIcon(int rightResId) {
            P.mRightResId = rightResId;
            return this;
        }

        public Builder setLeftIcon(int iconRes) {
            P.mLeftResId = iconRes;
            return this;
        }

        public Builder setTitleBackgroundColor(int bgColor) {
            P.bgColor = bgColor;
            return this;
        }

        public Builder setRightClickListener(View.OnClickListener rClickListener) {
            P.mRightClickListener = rClickListener;
            return this;
        }
        // 可覆盖左边点击事件的方法。
        public Builder setLeftClickListener(View.OnClickListener lClickListener) {
            P.mLeftClickListener = lClickListener;
            return this;
        }
        // ...

        @Override
        public DefaultNavigationBar builder() {
            DefaultNavigationBar navigationBar = new DefaultNavigationBar(P);
            return navigationBar;
        }

        public DefaultNavigationBar.Builder hideLeftIcon() {
            P.leftIconVisible = View.INVISIBLE;
            return this;
        }

        public static class DefaultNavigationParams extends
                AbsNavigationBar.Builder.AbsNavigationParams {
            // 2.存放所有效果
            public String mTitle;
            public String mRightText;
            public String mLeftText;
            public int mRightResId;
            public int mLeftResId;
            public View.OnClickListener mRightClickListener;
            public int leftIconVisible = View.VISIBLE;


            // 左边点击事件，有默认事件(关闭)。可以覆盖该方法。
            public View.OnClickListener mLeftClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 默认关闭当前页面
                    ((Activity) mContext).finish();
                }
            };
            public int bgColor; // 背景色

            public DefaultNavigationParams(Context context, ViewGroup parent) {
                super(context, parent);
            }
        }
    }
}
